package com.lanou.dllo.vc.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.easemob.chat.EMGroup;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.activity.ChatGroupActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/12/2.
 */
public class GroupChatAdapter extends BaseAdapter {

    private List<EMGroup> groups;
    private LayoutInflater inflater;
    private Context context;

    public GroupChatAdapter(Context c) {
        this.context = c;
        inflater = LayoutInflater.from(c);
    }

    @Override
    public int getCount() {
        return groups != null && groups.size() > 0 ? groups.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return groups.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        Log.i("lanou", "int---->" + getCount());
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_group_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final EMGroup group = groups.get(position);
        if (group.getGroupName()!=null&&group.getGroupName().length()>0) {
            holder.groupId.setText(group.getGroupName());
        }else {
            holder.groupId.setText(group.getGroupId());
        }

        holder.groupId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ChatGroupActivity.class);
                intent.putExtra("group", group.getGroupId());
                context.startActivity(intent);
            }
        });

        return convertView;
    }


    public void addData(List<EMGroup> datas) {
        this.groups = datas;

        notifyDataSetChanged();
    }

    class ViewHolder {
        @Bind(R.id.groupIdTv)
        TextView groupId;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
